package org.xqp.hrm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.xqp.hrm.constants.BaseConstants;
import org.xqp.hrm.constants.RedisConstants;
import org.xqp.hrm.domain.Login;
import org.xqp.hrm.domain.VipBase;
import org.xqp.hrm.domain.VipUser;
import org.xqp.hrm.dto.UserRegisterDto;
import org.xqp.hrm.feign.client.LoginFeignClient;
import org.xqp.hrm.mapper.VipBaseMapper;
import org.xqp.hrm.mapper.VipUserMapper;
import org.xqp.hrm.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.xqp.hrm.to.SendVerifyCodeTo;
import org.xqp.hrm.util.AjaxResult;
import org.xqp.hrm.util.AssertUtil;
import org.xqp.hrm.util.BitStatesUtils;

import java.util.Date;

/**
 *
 *                                          账号注册实现
 * 1.判断参数
 * 2.判断手机是否已经被注册
 * 3.判断手机验证码
 * 3.1.获取Redis中的手机验证码
 * 3.2.和传入的手机验证码比较
 * 4.保存Login
 * 5.保存User
 * 6.保存Base
 * 7.删除手机验证码
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate ;
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Override
    public void register(UserRegisterDto dto) {

        //* 1.判断参数
        String mobile = dto.getMobile(); // 获取电话号码
        String password = dto.getPassword(); // 获取密码
        String smsCode = dto.getSmsCode(); // 获取电话短信验证码
        Integer regChannel = dto.getRegChannel(); // 获取注册方式

        // 判断手机号是否为空
        AssertUtil.isNotEmpty(mobile, "无效的手机号");
        //根据手机号去数据库中查询
        Wrapper<VipUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",mobile); // 根据前端传过来的电话去数据库找相同的phone 这是一个where条件
        VipUser vipUser = super.selectOne(wrapper); //
        //* 2.判断手机是否已经被注册
        AssertUtil.isNull(vipUser,"手机号已注册" );

        //* 3.判断手机验证码
        //* 3.1.获取Redis中的手机验证码
        String key = String.format(RedisConstants.KEY_SMS_CODE,mobile); // 先提前获取key
        SendVerifyCodeTo sendVerifyCodeTo = (SendVerifyCodeTo) redisTemplate.opsForValue().get(key); // 根据key获取reids中的SendVerifyCodeTo对象
        AssertUtil.isNotNull(sendVerifyCodeTo,"无效的手机验证码，请重新发送" );
        //* 3.2.和传入的手机验证码比较
        AssertUtil.isEqualsIgnoreCase(sendVerifyCodeTo.getCode(), smsCode, "手机验证码错误");

        //* 4.保存Login
        Login login = new Login();
        login.setUsername(mobile); // 将电话号码设置为username
        //学了Oauth2在做
        login.setPassword(password); // 设置password
        login.setType(BaseConstants.Login.TYPE_USER); // 设置是前台用户
        AjaxResult ajaxResult = loginFeignClient.save(login); //  调用fen将login存入数据库
        AssertUtil.isTrue(ajaxResult.isSuccess(),"保存登录表失败" );
        AssertUtil.isNotNull(ajaxResult.getResultObj(),"保存登录表失败" );
        // 获取通过fen返回给我们的login登录ID
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //* 5.保存User
        VipUser user = new VipUser(); // 创建user对象
        user.setCreateTime(new Date().getTime()); //
        user.setPhone(mobile);
        user.setNickName(mobile);
        //默认注册成功的标识
        long bitState = BitStatesUtils.addState(user.getBitState(), BitStatesUtils.OP_ACTIVED);
        //绑定了手机的标识
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        user.setBitState(bitState);
        user.setSecLevel(0);
        user.setLoginId(loginId);
        super.insert(user);

        //* 6.保存Base
        VipBase base = new VipBase();
        base.setCreateTime(user.getCreateTime());
        base.setSsoId(user.getId());
        //注册渠道
        base.setRegChannel(regChannel);
        // 创建时间
        base.setRegTime(base.getCreateTime());
        vipBaseMapper.insert(base);
        //* 7.删除手机验证码
        redisTemplate.delete(key);

    }

}